#include "friendlist.h"
#include "ui_friendlist.h"
#include"QTableView"
#include <QStandardItemModel>
#include<QAbstractItemView>
#include"QPushButton"
#include"addsuccess.h"
friendlist * friendlist::myfriendlist=nullptr;
friendlist::friendlist(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::friendlist)
{
    ui->setupUi(this);
        tableView = new QTableView(ui->tableView);
        tableView->setMinimumSize(700, 500);
        tableView->verticalHeader()->hide(); // hide row number
        tableModel = new QStandardItemModel(ui->tableView);
        tableView->setModel(tableModel);

        tableView2 = new QTableView(ui->tableView_2);
          tableView2->setMinimumSize(700, 500);
          tableView2->verticalHeader()->hide(); // hide row number
          tableModel2 = new QStandardItemModel(ui->tableView_2);
          tableView2->setModel(tableModel2);



}

friendlist::~friendlist()
{
    delete ui;
}

void friendlist::GetTCPsocket(TCPSocket *Socket){

//   mysocket=Socket;
}
friendlist:: friendlist(QString id,QString password){
    ui->setupUi(this);

}

void friendlist::on_chatbutton_clicked()
{
    QPushButton *button=(QPushButton*)sender();

 QString gid="";
 QString id="";

 id=button->property("id").toString();
    Account::getInstance()->getGid(id,gid);
emit sendid(gid);
emit openchat(gid);

}












void friendlist::addfriend(QString id, QString name){

//    myfriendnum++;
}
void friendlist::getIdAndName(){


Account::getInstance()->getFriend(NameAndId);
qDebug()<<"Map"<<NameAndId;
myfriendnum=NameAndId.size();
qDebug()<<"friend num"<<myfriendnum;
     // set columns
     QStringList columnTitles;
     columnTitles << "好友序号" << "好友id" << "好友昵称" << "通讯";
     tableModel->setHorizontalHeaderLabels(columnTitles);
     tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); // column fit the contents

//      add contents

           QMap<QString,QString>::Iterator it = NameAndId.begin();
           myfriendnum=0;
             while(it!=NameAndId.end()){

                 tableModel->setItem(myfriendnum, 0, new QStandardItem(QString::number(myfriendnum + 1)));
                 tableModel->setItem(myfriendnum, 1, new QStandardItem(QString(it.key()).arg(myfriendnum)));
                 tableModel->setItem(myfriendnum, 2, new QStandardItem( it.value()));
                  // add button to the last column
                  QPushButton *chatbutton = new  QPushButton("开始聊天");
                // chatbutton->id= it.key();
                  // set custom property
                  chatbutton->setProperty("序号", myfriendnum); // set custom property
                  chatbutton->setProperty("id", it.key());
                  chatbutton->setProperty("昵称",  it.value());
                  qDebug()<<"key"<<it.key()<<it.value();
                  // set click event
                connect(chatbutton, SIGNAL(clicked()), this, SLOT(on_chatbutton_clicked()));
                  // notice every time insert the button at the last line
                  tableView->setIndexWidget(tableModel->index(tableModel->rowCount() - 1, 3), chatbutton);
                myfriendnum++;
                 it++;
             }


}
void friendlist::on_chatbutton_clicked2()
{
    QPushButton *button=(QPushButton*)sender();

emit(sendgroupid(button->property("id").toString()));
emit(sendOpenGroup(button->property("id").toString()));
qDebug()<<"hhhhh"<<button->property("id").toString();
}



void  friendlist::getGroupIdAndName(){
//    GroupNameAndId=;
Account::getInstance()->getGroup(GroupNameAndId);
         // set columns
         QStringList columnTitles;
         columnTitles << "群身份" << "群id" << "群昵称" << "群通讯";
         tableModel2->setHorizontalHeaderLabels(columnTitles);
     //    tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); // column fit the contents

         // add contents

               QMap<QString,QString>::Iterator it = GroupNameAndId.begin();
                groupnum=0;
                 while(it!= GroupNameAndId.end()){
                     tableModel2->setItem(groupnum, 0, new QStandardItem("群成员"));
                     tableModel2->setItem(groupnum, 1, new QStandardItem(QString( it.key()).arg(groupnum)));
                     tableModel2->setItem(groupnum, 2, new QStandardItem( it.value()));
                      // add button to the last column
                      QPushButton *groupchatbutton = new  QPushButton("开始聊天");
                    // chatbutton->id= it.key();
                      // set custom property
                     groupchatbutton->setProperty("群身份", "群友"); // set custom property
                     groupchatbutton->setProperty("id", it.key());
                     groupchatbutton->setProperty("昵称",  it.value());
                      // set click event
                    connect(groupchatbutton, SIGNAL(clicked()), this, SLOT(on_chatbutton_clicked2()));
                      // notice every time insert the button at the last line
                      tableView2->setIndexWidget(tableModel2->index(tableModel2->rowCount() - 1, 3),groupchatbutton);
               groupnum++;
                     it++;
                 }




}

void friendlist::on_addfriend_clicked()
{

    emit(ToAddFriend());

}




















void friendlist::on_pushButton_clicked()
{

//    myfriendnum=NameAndId.size();
//    qDebug()<<"fri"<<myfriendnum;
//         // set columns
//         QStringList columnTitles;
//         columnTitles << "好友序号" << "好友id" << "好友昵称" << "通讯";
//         tableModel->setHorizontalHeaderLabels(columnTitles);
//     //    tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); // column fit the contents

//         // add contents

//               QMap<QString,QString>::Iterator it = NameAndId.begin();
//               myfriendnum=0;
//              while(it!=NameAndId.end()){
//                     qDebug()<<" hhhh";
//                     tableModel->setItem(myfriendnum, 0, new QStandardItem(QString::number(myfriendnum + 1)));
//                     tableModel->setItem(myfriendnum, 1, new QStandardItem(QString(it.key()).arg(myfriendnum)));
//                     tableModel->setItem(myfriendnum, 2, new QStandardItem( it.value()));
//                      // add button to the last column
//                      QPushButton *chatbutton = new  QPushButton("开始聊天");
//                    // chatbutton->id= it.key();
//                      // set custom property
//                      chatbutton->setProperty("序号", myfriendnum); // set custom property
//                      chatbutton->setProperty("id", it.key());
//                      chatbutton->setProperty("昵称",  it.value());
//                      qDebug()<<"key"<<it.key()<<it.value();
//                      // set click event
//                    connect(chatbutton, SIGNAL(clicked()), this, SLOT(on_chatbutton_clicked()));
//                      // notice every time insert the button at the last line
//                      tableView->setIndexWidget(tableModel->index(tableModel->rowCount() - 1, 3), chatbutton);
//                    myfriendnum++;
//                     it++;
//              }

}

void friendlist::on_addgroup_clicked()
{
    emit(creategroup());
}

void friendlist::on_createButton_clicked()
{
    emit(creategroup());
}
